<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-01 14:14:10
 * ------------------------------------------------ *
 * 商品                                        *
 * ------------------------------------------------ */
namespace Admin\Model;


class GoodsModel extends CommonModel{



	//获取列表

	public function getGoods(){
		$a=M('goods');
		//$result = $a->where('shopFlag!=-1')->select();
		$result =$a->join('as a left join '.C("DB_PREFIX").'goods_cats as b on a.goodsCatId1=b.catId')->join('left join '.C("DB_PREFIX").'shops as c on a.shopId=c.shopId')->field('a.*,b.catName,c.shopName')->where('a.goodsFlag=1')->select();
		if(false!==$result){
			return $result;
		}
		return false;

	}

	//获取待审核列表
	public function getGoodsPendding(){

		$a=M('goods');
		//$result = $a->where('shopFlag!=-1')->select();
		$result =$a->join('as a left join '.C("DB_PREFIX").'goods_cats as b on a.goodsCatId1=b.catId')->join('left join '.C("DB_PREFIX").'shops as c on a.shopId=c.shopId')->field('a.*,b.catName,c.shopName')->where('a.goodsFlag=1 and a.goodsStatus=0')->select();
		if(false!==$result){
			return $result;
		}
		return false;
	}
 
//获取商品信息

	 public function getView(){
	 	$m = M('goods');
	 	$id = (int)I('goodsId',0);
		$goods = $m->where("goodsId=".$id)->find();
		//相册
		$m = M('goods_gallerys');
		$goods['gallery'] = $m->where('goodsId='.$id)->select();
		//商城分类
		$sql = "select c1.catName goodsName1,c2.catName goodsName2,c3.catName goodsName3
		        from __PREFIX__goods_cats c3 , __PREFIX__goods_cats c2,__PREFIX__goods_cats c1
		        where c3.parentId=c2.catId and c2.parentId=c1.catId and c3.catId=".$goods['goodsCatId3'];
		$rs = $this->query($sql);
		$goods['goodsCats'] = $rs[0];
		//店铺分类
		$sql = "select c1.catName goodsName1,c2.catName goodsName2
		        from __PREFIX__shops_cats c2 ,__PREFIX__shops_cats c1
		        where c2.parentId=c1.catId and c2.catId=".$goods['shopCatId2'];
		$rs = $this->query($sql);
		$goods['shopCats'] = $rs[0];
		//属性获取
		if($goods['attrCatId']>0){
			$sql = "select catName from __PREFIX__attribute_cats where catId=".$goods['attrCatId'];
			$rs = $this->query($sql);
		    $goods['attrCatName'] = $rs[0]['catName'];
		    
			//获取规格属性
			$sql = "select ga.attrVal,ga.attrPrice,ga.attrStock,a.attrId,a.attrName,a.isPriceAttr,ga.isRecomm
			            ,ga.isRecomm from __PREFIX__attributes a 
			            left join __PREFIX__goods_attributes ga on ga.attrId=a.attrId and ga.goodsId=".$id." where  
						a.attrFlag=1 and a.catId=".$goods['attrCatId']." and a.shopId=".$goods['shopId'];
			$attrRs = $m->query($sql);
			if(!empty($attrRs)){
				$priceAttr = array();
				$attrs = array();
				foreach ($attrRs as $key =>$v){
					if($v['isPriceAttr']==1){
						$goods['priceAttrName'] = $v['attrName'];
						$priceAttr[] = $v;
					}else{
						$v['attrContent'] = $v['attrVal'];
						$attrs[] = $v;
					}
				}
				$goods['priceAttrs'] = $priceAttr;
				$goods['attrs'] = $attrs;
			}
		}
		return $goods;
	 }


// 修改商品状态

	 public function goodsStatus(){
	 	$m = M('goods');
	 	$id = (int)I('id',0);
	 	$m->goodsStatus = (int)I('status',0);
		$rs = $m->where('goodsId='.$id)->save();
		if(false !== $rs){
			$a=M('goods');
			$goods=$a->join('as g left join '.C("DB_PREFIX").'shops as s on g.shopId=s.shopId and g.goodsId='.$id)->field()->select();
			$msg = "";
			if(I('status',0)==0){
				$msg = "商品[".$goods[0]['goodsName']."]已被商城下架";
			}else{
				$msg = "商品[".$goods[0]['goodsName']."]已通过审核";
			}
			$mdata = array(
				'msgType' => 0,
				'sendUserId' => session('admin.managerId'),
				'receiveUserId' => $goods[0]['userId'],
				'msgContent' => $msg,
				'createTime' => date('Y-m-d H:i:s'),
				'msgStatus' => 0,
				'msgFlag' => 1,
			);
			M('messages')->add($mdata);
			return true;
		}
		return false;
	 }

	 public function setStatus(){
	 	$m = M('goods');
	 	$id = (int)I('id',0);
	 	$m->goodsStatus = (int)I('status',0);
		$rs = $m->where('goodsId='.$id)->save();
		if(false !== $rs){
			$a=M('goods');
			$goods=$a->join('as g left join '.C("DB_PREFIX").'shops as s on g.shopId=s.shopId and g.goodsId='.$id)->field('g.goodsName,s.userId')->find();

			$msg = "";
			if(I('status',0)==0){
				$msg = "商品[".$goods['goodsName']."]已被商城下架";
			}else{
				$msg = "商品[".$goods['goodsName']."]已通过审核";
			}
			$mdata = array(
				'msgType' => 0,
				'sendUserId' => session('admin.managerId'),
				'receiveUserId' => $goods['userId'],
				'msgContent' => $msg,
				'createTime' => date('Y-m-d H:i:s'),
				'msgStatus' => 0,
				'msgFlag' => 1,
			);
			M('messages')->add($mdata);
			return true;
		}
		return false;
	 }

// 获取待审核的商品数量

	 public function getPenddingGoodsNum(){
	 	$m = M('goods');
	 	$num=$m->where('goodsStatus=0 and goodsFlag=1')->count();
	 	return $num;
	 }

// 批量修改精品状态

	 public function bestStatus(){
	 	$m = M('goods');
	 	$id = I('id',0);
	 	$m->isAdminBest = (int)I('status',0);
		$rs = $m->where('goodsId in('.$id.")")->save();
		if(false !== $rs){
			return true;
		}
		return false;
	 }
   
// 批量修改推荐状态

	 public function recomStatus(){
	 	$m = M('goods');
	 	$id = I('id',0);
	 	$m->isAdminRecom = (int)I('status',0);
		$rs = $m->where('goodsId in('.$id.")")->save();
		if(false !== $rs){
			return true;
		}
		return false;
	 }
}